<?php
class fvCookie
{
	private $_name  = "";
	private $_val   = array();
	private $_expires;
	private $_dir   = '/';    // all dirs
	private $_site  = '';

	function __construct($name='krshop', $expires="", $dir="/", $site = '')
	{
		$this->_name=$name;
		if($expires){
			$this->_expires=$expires;
		}
		else{
			$this->_expires=time() + 60*60*24*30*12; // ~12 months
		}

		$this->_dir=$dir;
		$this->_site=$site;
		$this->_val = $_COOKIE[$this->_name] ? unserialize(stripslashes($_COOKIE[$this->_name])) : array();
		$this->extract();
	}

	public function extract()
	{
		return $this->_val;
	}

	function put($var, $value)
	{
		if (is_array($value)){
			foreach ($value as $k=>$v){
				if ($v){
					$this->_val[$var][$k] = $v;
				}
				else{
					unset($this->_val[$var][$k]);
				}
			}
		}
		else{
			$this->_val[$var] = $value;
		}
		$this->set();
	}
	
	function add($var, $value)
	{
		if (is_array($value)){
			foreach ($value as $k=>$v){
				$this->_val[$var][$k] += $v;
			}
		}
		else{
			$this->_val[$var] += $value;
		}
		$this->set();
	}

	function clear()
	{
		$this->_val=array();
	}

	function set()
	{
		setcookie($this->_name, serialize($this->_val), $this->_expires, $this->_dir, $this->_site);
	}
}
?>